#ifndef USER_ACCOUNT_HPP
#define USER_ACCOUNT_HPP

#include "transaction_record.hpp"
#include <QString>
#include <QVector>

struct Position {
  QString stockCode;
  QString stockName;
  int shares;
  double avgPrice;
  double currentPrice; // 当前价格，用于计算盈亏

  double getTotalCost() const;
  double getCurrentValue() const;
  double getProfit() const;
  double getProfitPercent() const;
};

class UserAccount {
public:
  UserAccount() = default;
  UserAccount(const QString &username, double initialBalance = 0.0);

  // 账户操作
  QString getUsername() const;
  double getBalance() const;
  void setBalance(double balance);

  // 交易功能
  bool buy(const QString &stockCode, const QString &stockName, int shares,
           double price);
  bool sell(const QString &stockCode, const QString &stockName, int shares,
            double price);

  // 持仓相关
  QVector<Position> &getPositions();
  const QVector<Position> &getPositions() const;
  Position *getPosition(const QString &stockCode);
  void updatePositionPrice(const QString &stockCode, double newPrice);

  // 获取总资产（现金 + 持仓市值）
  double getTotalAssets() const;

  // 交易记录
  const QVector<TransactionRecord> &getTransactionHistory() const;

private:
  QString m_username;
  double m_balance;                                // 可用资金
  QVector<Position> m_positions;                   // 持仓
  QVector<TransactionRecord> m_transactionHistory; // 交易记录
};

#endif // USER_ACCOUNT_HPP
